// ----------------
// Project:
// ESA Elevator
// ----------------
//
// ----------------
// Group: 163
// 2358569: Pascal Klink
// 2649456: Lukas Kreh
// ----------------
//
// Description:
// ----------------
// elevator floor-request-interface 
//
// Version History:
// ----------------
// 140116: set parameter FLOORS to 30
// 140116: set parameter FLOOR_BITS to 5
// 140116: changed comment of ENLIGHT_BUTTONS

`timescale 1ns / 1ns

module floor_request_if

          #(parameter FLOORS     = 30,
                      FLOOR_BITS =  5)


          (input  wire                          CLK,
           input  wire                          RESET,    

           input  wire [(FLOOR_BITS-1)      :0] CURRENT_FLOOR,
           input  wire                          HALTED,
           
           output wire [(FLOORS*2)-1:0]         ENLIGHT_BUTTONS, //only (FLOORS-1)*2 bits would be needed, but easier to handle
           
           input  wire [(FLOORS*2)-1:0]         FLOOR_REQUEST_IN,
           output wire [(FLOORS*2)-1:0]         FLOOR_REQUEST_OUT);
      
/* =============================INSERT CODE HERE======================================*/ 

reg[(FLOORS*2)-1:0] requested_floors;
assign ENLIGHT_BUTTONS=requested_floors;
assign FLOOR_REQUEST_OUT=requested_floors;
always@(posedge CLK)begin
	//reset request pipeline to 0
	if(RESET)begin
		requested_floors[(FLOORS*2)-1:0]<=0;
	end
	else begin
		//request pipeline
		if(requested_floors!=FLOOR_REQUEST_IN)begin
			requested_floors<=(requested_floors | FLOOR_REQUEST_IN);
		end
		if(HALTED)begin 
			//then, if a request from CURRENT_FLOOR is in the list, delete 'em.
			//  EG |  1. | ... | OG
			// 0 1 | 2 3 | ... | n  array-position
			//   u | d u | d u | d
			if(requested_floors[CURRENT_FLOOR*2] || requested_floors[(CURRENT_FLOOR*2)+1])begin
				requested_floors[(CURRENT_FLOOR*2)+1]<=1'b0;	//up
				requested_floors[CURRENT_FLOOR*2]<=1'b0;		//down
			end
		end
	end
end

/* ====================================================================================*/

endmodule
